﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using System.Windows.Threading;
using System.Diagnostics;
using Microsoft.Phone.Tasks;

namespace NapThe
{
    public partial class NapTheQplay : UserControl,INapTheQuaTang
    {
        public DispatcherTimer newTimer = new DispatcherTimer();
        public static string maTheQuaTang = "";
        Grid MainLayoutRoot;
        NapXu napxu;
        public NapTheQplay(Grid MainLayoutRoot, NapXu napxu)
        {
            InitializeComponent();
            this.MainLayoutRoot = MainLayoutRoot;
            this.MainLayoutRoot.Children.Add(this);
            this.napxu = napxu;
        }
     

        public void OnVisible()
        {
            this.Visibility = Visibility.Visible;
            
            radioButton4.IsChecked = true;
            rectangle1.Width = 0;
            textBlock8.Text = "";
            newTimer.Tick -= OnTimerTick;
            //newTimer.Start();
            newTimer.Interval = TimeSpan.FromMilliseconds(150);
            newTimer.Tick += OnTimerTick;
            if (!"".Equals(maTheQuaTang))
            {
                tbSeRi.Text = maTheQuaTang;
            }
            
        }
        public void OnCollapsed()
        {
            this.Visibility = Visibility.Collapsed;
            newTimer.Tick -= OnTimerTick;
            newTimer.Stop();
        }
        private void SendMessage(object sender, RoutedEventArgs e)
        {
            SmsComposeTask smsComposeTask = new SmsComposeTask();
            smsComposeTask.To = SMSModel.address;
            smsComposeTask.Body = SMSModel.content;
            smsComposeTask.Show(); // this will invoke the native sms edtior
        }
        public void OnTimerTick(object sender, EventArgs e)
        {
            
            if (NapTheDienThoaiController.counter >= 100)
            {
                newTimer.Stop();
                //IEnumerable<BackgroundTransferRequest> a = BackgroundTransferService.Requests;
                //if (a != null)
                //{
                //    foreach (var request in a)
                //    {
                //        Controller.DownloadController.RemoveTransferRequest(request.RequestId);
                //    }
                //}

                NapTheDienThoaiController.counter = 0;
                NapTheQuaTangController.isRunning = false;
                //Controller.DownloadController.count = 1;
                MessageBox.Show("Tạm thời không kết nối được mạng Internet!");
            }
            else
            {
                Debug.WriteLine("time check running" + NapTheDienThoaiController.counter);
                textBlock8.Text = NapTheDienThoaiController.counter + "% completed";
                rectangle1.Width = (double)(8 * NapTheDienThoaiController.counter);
                NapTheDienThoaiController.counter++;
                
            }
        }
        private void textWhenMouseLeftDown1(object sender, MouseButtonEventArgs e)
        {
            if (tbSeRi.Text.Contains("Nhập mã số thẻ"))
            {
                tbSeRi.Text = "";
            }
        }
        private void key_Down_Listener(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!NapTheDienThoaiController.isRunning
                    && !NapTheQuaTangController.isRunning
                )
                {
                    NapTheQuaTangController n = new
                    NapTheQuaTangController();
                   
                    n.process(this);
                    //System.IO.IsolatedStorage s;
                }
                else
                {
                    MessageBox.Show("Hệ thống đang nạp thẻ cào");
                }
            }
        }
        public void Click_ChangeToNapTheDienThoai(object sender, RoutedEventArgs e)
        {
            
           
            napxu.naptheDienThoai.OnVisible();
            this.OnCollapsed();
            RadioButton radio = (RadioButton)sender;
            if (radio.Name.Equals("radioButton1"))
            {
                NapTheDienThoaiModel.intNhaMang = 1;
                napxu.naptheDienThoai.radioButton1.IsChecked = true;
                return;
            }
            if (radio.Name.Equals("radioButton2"))
            {
                napxu.naptheDienThoai.radioButton2.IsChecked = true;
                NapTheDienThoaiModel.intNhaMang = 2;
                return;
            }
            if (radio.Name.Equals("radioButton3"))
            {
                napxu.naptheDienThoai.radioButton3.IsChecked = true;
                NapTheDienThoaiModel.intNhaMang = 3;
                return;
            }
         //   
        }
        private void btnNapThe_Click(object sender, RoutedEventArgs e)
        {


            if (!NapTheDienThoaiController.isRunning
                && !NapTheQuaTangController.isRunning
                )
            {
                setResource("textBlock24", "BGStyle3", "BGStyle3");
               NapTheQuaTangController n = new NapTheQuaTangController();
                n.process(this);
               
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
        }
        private void btnKhac_Click(object sender, RoutedEventArgs e)
        {
            if (!NapTheDienThoaiController.isRunning
                  && !NapTheQuaTangController.isRunning
                  )
            {
                setResource("textBlock25", "BGStyle3", "BGStyle3");
                OnCollapsed();

                napxu.khacScreen.OnVisible();
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
            
        }
        private void btnKhong_Click(object sender, RoutedEventArgs e)
        {
            if (!NapTheDienThoaiController.isRunning
                  && !NapTheQuaTangController.isRunning
                  )
            {
                setResource("textBlock26", "BGStyle3", "BGStyle3");
                this.Visibility = Visibility.Collapsed;
            }
            else
            {
                MessageBox.Show("Hệ thống đang nạp thẻ cào");
            }
                
        }
        private void DownClick(object sender, MouseEventArgs e)
        {
            TextBlock t = (TextBlock)sender;
            setResource(t.Name, "BGStyle4", "BGStyle3");
        }
        public void setResource(string name, string source1, string source2)
        {
            if (!source1.Equals(source2))
            {
                resetResource(name, source2);
            }
            if (button12.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source1];
                return;
            }
            if (button13.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source1];
                return;
            }
            if (button14.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source1];
                return;
            }

            if (textBlock26.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source1];
                return;
            }
            if (textBlock25.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source1];
                return;
            }
            if (textBlock24.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source1];
                return;
            }
        }

        private void resetResource(string name, string source)
        {
            if (!textBlock26.Name.Equals(name))
            {
                button14.Style = (Style)this.Resources[source];
                //return;
            }
            if (!textBlock25.Name.Equals(name))
            {
                button13.Style = (Style)this.Resources[source];
                //return;
            }
            if (!textBlock24.Name.Equals(name))
            {
                button12.Style = (Style)this.Resources[source];
                //return;
            }

        }
        public void setRectangle1(double x)
        {
            this.rectangle1.Width = x;
        }
        public void setTextBlock(string name, string content)
        {
            if (textBlock8.Name.Equals(name))
            {
                textBlock8.Text = content;
                //maTheQuaTang = content;
            }
        }

        public string getMaThe()
        {
            return tbSeRi.Text;
        }

        public void setMaThe(string content)
        {
            this.tbSeRi.Text = content;
            maTheQuaTang = content;
        }


        public void processTimer(string nameOfAction)
        {
            if ("Start".Equals(nameOfAction))
            {
                this.newTimer.Start();
            }
            if ("Stop".Equals(nameOfAction))
            {
                this.newTimer.Stop();
            }
        }

        public void navigateWhenSuccess()
        {
            this.Visibility = Visibility.Collapsed;
            OnCollapsed();
        }

        public void navigateWhenFail()
        {
            NapXu.report.OnNavigatedTo();
        }

    }
}
